package cn.com.db.service;


import cn.com.db.dto.Arg;
import cn.com.db.dto.DiyWorkArg;
import cn.com.db.model.*;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class DiyWorkService extends BaseService {


    @Transactional
    public void createDiyWork(DiyWork diyWork,DiyWorkArg diyWorkArg)throws Exception{
        diyWorkDao.createDiyWork(diyWork);
        List<DiyEmbryo> diyEmbryos=new ArrayList<>();
        List<DiySurface> diySurfaces=new ArrayList<>();
        List<DiyParts> diyPartses=new ArrayList<>();
        List<DiyDeck> diyDecks=new ArrayList<>();
        for (Arg arg:diyWorkArg.getEmbryo()){
            StoreEmbryo storeEmbryo=storeEmbryoDao.getStoreEmbryoById(arg.getId());
            if (storeEmbryo==null||storeEmbryo.getDel()){
                throw new Exception("创建diy蛋糕失败,选择了不存在或已下架的蛋糕胚,请重新选择!");
            }
            DiyEmbryo diyEmbryo=new DiyEmbryo();
            diyEmbryo.setId(UidUtil.createUUID());
            diyEmbryo.setCount(arg.getCount());
            diyEmbryo.setDiywork(diyWork.getId());
            diyEmbryo.setStoreEmbryo(storeEmbryo);
            diyEmbryo.setCreated_at(TimeUtil.getNowTimestamp());
            diyEmbryos.add(diyEmbryo);
        }
        for (Arg arg:diyWorkArg.getSurface()){
            StoreSurface storeSurface=storeSurfaceDao.getStoreSurfaceById(arg.getId());
            if (storeSurface==null||storeSurface.getDel()){
                throw new Exception("创建diy蛋糕失败,选择了不存在或已下架的表层,请重新选择");
            }
            DiySurface diySurface=new DiySurface();
            diySurface.setId(UidUtil.createUUID());
            diySurface.setCount(arg.getCount());
            diySurface.setDiywork(diyWork.getId());
            diySurface.setStoreSurface(storeSurface);
            diySurface.setCreated_at(TimeUtil.getNowTimestamp());
            diySurfaces.add(diySurface);
        }
        for (Arg arg:diyWorkArg.getDeck()){
            StoreDeck storeDeck=storeDeckDao.getStoreDeckById(arg.getId());
            if (storeDeck==null||storeDeck.getDel()){
                throw new Exception("创建diy蛋糕失败,选择了不存在或已下架的装饰,请重新选择");
            }
            DiyDeck diyDeck=new DiyDeck();
            diyDeck.setId(UidUtil.createUUID());
            diyDeck.setCount(arg.getCount());
            diyDeck.setDiywork(diyWork.getId());
            diyDeck.setStoreDeck(storeDeck);
            diyDeck.setCreated_at(TimeUtil.getNowTimestamp());
            diyDecks.add(diyDeck);
        }
        for (Arg arg:diyWorkArg.getParts()){
            StoreParts storeParts=storePartsDao.getStorePartsById(arg.getId());
            if (storeParts==null||storeParts.getDel()){
                throw new Exception("创建diy蛋糕失败,选择了不存在或已下架的配件,请重新选择");
            }
            DiyParts diyParts=new DiyParts();
            diyParts.setId(UidUtil.createUUID());
            diyParts.setCount(arg.getCount());
            diyParts.setDiywork(diyWork.getId());
            diyParts.setStoreParts(storeParts);
            diyParts.setCreated_at(TimeUtil.getNowTimestamp());
            diyPartses.add(diyParts);
        }
        if (diyDecks!=null&&diyDecks.size()>0){
            diyDeckDao.batchCreate(diyDecks);
        }
        if (diyEmbryos!=null&&diyEmbryos.size()>0){
            diyEmbryoDao.batchCreate(diyEmbryos);
        }
        if (diySurfaces!=null&&diySurfaces.size()>0){
            diySurfaceDao.batchCreate(diySurfaces);
        }
        if (diyPartses!=null&&diyPartses.size()>0){
            diyPartsDao.batchCreate(diyPartses);
        }
    }

    public DiyWork getDiyWorkById(String id){
        return diyWorkDao.getDiyWorkById(id);
    }

    public List<DiyWork> getUserDiyWork(String userId,Integer page,Integer count){
        return diyWorkDao.getUserDiyWork(userId,(page-1)*count,count);
    }

    public Integer countUserDiyWork(String userId){
        return diyWorkDao.countUserDiyWork(userId);
    }

    public BigDecimal countDiyWorkPrice(String id){
        BigDecimal sum=new BigDecimal(0);
        if (StringUtils.isBlank(id)){
            return sum;
        }
        DiyWork diyWork=diyWorkDao.getDiyWorkById(id);
        if (diyWork==null){
            return sum;
        }
        if (diyWork.getEmbryos()!=null&&diyWork.getEmbryos().size()!=0){
            for (DiyEmbryo embryo:diyWork.getEmbryos()){
                sum=sum.add(embryo.getStoreEmbryo().getPrice());
            }
        }
        if (diyWork.getDecks()!=null&&diyWork.getDecks().size()!=0){
            for (DiyDeck deck:diyWork.getDecks()){
                sum=sum.add(deck.getStoreDeck().getPrice());
            }
        }
        if (diyWork.getSurfaces()!=null&&diyWork.getSurfaces().size()!=0){
            for (DiySurface surface:diyWork.getSurfaces()){
                sum=sum.add(surface.getStoreSurface().getPrice());
            }
        }
        if (diyWork.getPartses()!=null&&diyWork.getPartses().size()!=0){
            for (DiyParts parts:diyWork.getPartses()){
                sum=sum.add(parts.getStoreParts().getPrice());
            }
        }
        if (diyWork.getTaste()!=null){
            sum=sum.add(diyWork.getTaste().getPrice());
        }
        return sum;
    }
}
